<template>
  <div class="container">
    <el-card class="box-card">
      <questions-discipline @searchhh="aSearchList" @clearlist="aclearlist" />
      <el-alert
        title="数据一共7条"
        type="info"
        show-icon
        close-text="1"
        class="el_alert_mb"
      >
      </el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="试题编号" width="120" prop="number">
        </el-table-column>
        <el-table-column label="学科" width="170" prop="subject">
        </el-table-column>
        <el-table-column label="目录" width="170" prop="catalog">
        </el-table-column>
        <el-table-column
          label="题型"
          width="170"
          :formatter="formatter"
          prop="questionType"
        >
        </el-table-column>
        <el-table-column
          label="题干"
          width="200"
          prop="question"
          :formatter="qusetionlist"
        >
        </el-table-column>
        <el-table-column
          label="录入时间"
          width="170"
          prop="addDate"
          :formatter="tansformTime"
        >
        </el-table-column>
        <el-table-column
          label="难度"
          width="170"
          prop="difficulty"
          :formatter="difficultylist"
        >
        </el-table-column>
        <el-table-column label="录入人" width="150" prop="creator">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-view"
              size="mini"
              circle
              plain
              @click="ShowDiaor(scope.$index, scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              circle
              plain
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="handleDelete(scope.$index, scope.row)"
              plain
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-check"
              size="mini"
              circle
              plain
              @click="SelectedSubmit(scope.$index, scope.row, yulandata)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="100"
          layout="prev, pager, next, sizes, jumper"
          :total="counts"
          background
          style="margin-top: 20px; text-align: right"
        >
        </el-pagination>
      </div>
    </el-card>
    <questions-mypreview
      :dialogFormVisible.sync="dialogFormVisible"
      :yulandata="yulandata"
    />
  </div>
</template>

<script>
import QuestionsDiscipline from '../components/questions-discipline.vue'
import QuestionsMypreview from '../components/questions-mypreview.vue'
import { list, detail, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { questionType, difficulty } from '@/api/hmmm/constants'
export default {
  components: {
    QuestionsDiscipline,
    QuestionsMypreview
  },
  data () {
    return {
      // 表单渲染
      tableData: [],
      counts: 0,
      // 分页器
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 0,
      questionType, // 转换题干
      difficulty, // 转换难度
      data: {
        pagesize: 5, // 当前页几条
        page: 1, // 当前第几页
        id: 0, // 当前题目id
        choiceState: 0 // 当前上架状态
      },
      dialogFormVisible: false, // 预览弹层是否显示
      yulandata: {
        answer: '',
        question: ''
      }
    }
  },
  async created () {
    await this.basisList()
    // this.questionTypework()
  },
  methods: {
    // 请求基础题库

    async basisList () {
      const {
        data: { items, counts }
      } = await list(this.data)
      this.tableData = items
      this.counts = counts
    },
    handleEdit (index, row) {
      console.log(index, row)
    },

    // 分页器
    async handleSizeChange (val) {
      this.data.pagesize = val
      this.basisList()
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.basisList()
    },
    // 题型转换
    formatter (row, column, cellValue, index) {
      const obj = questionType.find((item) => item.value == cellValue)
      return obj ? obj.label : '未知'
    },
    // 题干转换
    qusetionlist (row, column, cellValue, index) {
      const res = cellValue.replace(/<.*?>/gi, '')
      return res
    },
    // 转换时间
    tansformTime (row, column, cellValue, index) {
      const data = new Date(cellValue)
      const a = this.$moment(data).format('YYYY-MM-DD HH:mm:ss')
      return a
    },
    // 难度转换
    difficultylist (row, column, cellValue, index) {
      const obj = this.difficulty.find((item) => item.value == cellValue)
      return obj ? obj.label : '未知'
    },

    // 预览按钮

    async ShowDiaor (index, row) {
      this.data.id = row.id
      const { data } = await detail(this.data)
      this.yulandata = data

      this.dialogFormVisible = true
    },
    // 修改按钮

    // 删除按钮
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.data.id = row.id
          await remove(this.data)
          this.basisList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 提交精选库
    async SelectedSubmit (index, row, yulan) {
      this.data.id = row.id
      const a = await detail(this.data)
      this.yulandata = a.data
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.data.id = row.id
          this.data.choiceState = a.data.publishState + 1
          await choiceAdd(this.data)
          console.log(this.tableData)
          this.basisList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async aSearchList (val) {
      const {
        data: { items, counts }
      } = await list(val)
      this.tableData = items
      this.counts = counts
    },
    aclearlist () {
      this.basisList()
    }
  }
}
</script>

<style scoped>
.container {
  padding: 0 10px;
  margin: 10px 0;
}
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 100%;
}
.el_alert_mb {
  margin-bottom: 15px;
}
</style>
